/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import model.Conta;
import model.Extrato;
import model.Operacao;
import model.OperacaoSaque;
import persistence.ContaDAO;
import persistence.ExtratoDAO;
import persistence.OperacaoDAO;

/**
 *
 * @author Leandro
 */
public class OperacaoSaqueAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response, Integer contaLogada) throws IIOException {
       
        Double valor = Double.parseDouble(request.getParameter("saque"));

        float saldoConta = 0;
        if (valor.equals("") || contaLogada.equals("")) {
            try {
                response.sendRedirect("indexErro.xhtml");
            } catch (IOException ex) {
            }
        } else {
            Conta conta = new Conta();
            conta.setSaldo(valor);
            conta.setNumero(contaLogada);
            try {
                saldoConta = OperacaoDAO.getInstace().recuperaSaldo(conta);
            } catch (SQLDataException ex) {
            } catch (ClassNotFoundException ex) {
            } catch (SQLException ex) {
            }
            if (saldoConta >= valor) {
                try {
                    OperacaoDAO.getInstace().realizaSaque(conta);

                    Conta contaSalva = ContaDAO.getInstace().recuperaContaPorNumero(contaLogada);

                    Operacao operacao = new Operacao();
                    operacao.setTipoOperacao(new OperacaoSaque());
                    //  operacao = OperacaoDAO.getInstace().insereOperacao(operacao);
                    //PERSISTE O EXTRATO
                    Extrato extrato = new Extrato();
                    extrato.setConta(contaSalva);
                    extrato.setOperacao(operacao);
                    extrato.setStrOperacao(extrato.getOperacao().getTipoOperacao().getTipoOperacao());

                    GregorianCalendar calendar = new GregorianCalendar();
                    calendar.setTime(new Date());
                    int dia = calendar.get(GregorianCalendar.DAY_OF_MONTH);
                    extrato.setDia(dia);
                    extrato.setSaldo(contaSalva.getSaldo());
                    extrato.setValor(valor);
                    ExtratoDAO.getInstace().insereExtrato(extrato);

                    try {
                        request.setAttribute("numeroConta", contaLogada);
                        RequestDispatcher dispatcher = request.getRequestDispatcher("SucessoSaque.jsp");
                        dispatcher.forward(request, response);
                    } catch (ServletException ex) {
                        Logger.getLogger(OperacaoSaqueAction.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } catch (SQLDataException ex) {
                } catch (ClassNotFoundException ex) {
                } catch (SQLException ex) {
                } catch (IOException ex) {
                    try {
                        response.sendRedirect("indexErro.xhtml");
                    } catch (IOException ex1) {
                    }
                }
            } else {
                try {
                     request.setAttribute("numeroConta", contaLogada);
                     RequestDispatcher dispatcher = request.getRequestDispatcher("erroSaque.jsp");
                     dispatcher.forward(request, response);
                } catch (IOException ex) {
                } catch (ServletException ex) {
                    Logger.getLogger(OperacaoSaqueAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
